var shopDataType="VIRTUAL_GOODS";

$(function(){


    //初始化专业搜索条件
    $.initSubject({
        element:'subject-box',
        confirmBtn:false,
        removeBtn: false,
        style:false,
        notMultiple:true,
        classText:'form-control mb10',
        width:100,
        selectCallback:function(subjectIds){
            $("#subjectIds").val(subjectIds);
        }
    });
    queryAjaxVirtualList(1);

    $("body").on("click",".selectCouponList",function(){
         shopDataType="COUPON";
        queryAjaxCouponList(1);
    }).on("click",".giftCardList",function(){
        shopDataType="GIFT_CARD";
        queryAjaxGiftCardList(1);
    }).on("click",".courseList",function(){
        shopDataType="COURSE";
        queryAjaxCourseList(1);
    }).on('click', '#couponQuerySubmit', function () {
        //点击搜索查询从第一页开始查询
        queryAjaxCouponList(1);
    }).on('click', '#giftCardQuerySubmit', function () {
        //点击搜索查询从第一页开始查询
        queryAjaxGiftCardList(1);
        })
        .on('click', '#courseQuerySubmit', function () {
            //点击搜索查询从第一页开始查询
            queryAjaxCourseList(1);
        }).on('click','.coupon-shop-data',function () {
        var _this = $(this);
        var couponId = _this.data('id');
        $.ajax({
            url: baseloaction +'/admin/op/coupon/showCouponShopData',
            type:"post",
            data:{
                'couponId':couponId
            },
            success:function (result) {
                if(result.success==true){
                    var list = result.entity;
                    if(list==null || list.length ==0){
                        icModale(0,'优惠券没有设置使用范围');
                    }else{
                        var context = '';
                        $.each(list,function (index,shopData) {name="type"
                            context +='<div class="m-t m-b"><div class="m-r m-l">'+(index+1)+'、';
                            if(shopData.shopType=='COURSE'){
                                context +=shopData.shop.shopName +'【课程】'
                            }else if(shopData.shopType=='EXAM'){
                                context +=shopData.shop.shopName +'【试卷】'
                            }else if(shopData.shopType=='BOOK'){
                                context +=shopData.shop.shopName +'【图书】'
                            }else if(shopData.shopType=='MEMBER'){
                                context +=shopData.shop.shopName +'【会员】'
                            }else if(shopData.shopType=='QA'){
                                context +='限问答使用'
                            }else if(shopData.shopType=='ATTEND'){
                                context +='限旁听使用'
                            }
                            context +='</div></div>';
                        });
                        parent.layer.closeAll();
                        parent.layer.open({
                            type: 1,
                            area: ['620px', '520px'],
                            skin: 'layui-layer-demo',
                            title: '查看优惠券使用范围',
                            content:context ,
                            btn: '关闭',
                            yes: function (index, layero) {
                                parent.layer.close(index)
                            }
                        });
                    }
                }else{
                    icModale(0,result.message);
                }
            },
            error:function (error) {
                icModale(0,'系统错误，查看失败');
            }
        });
    }).on("click",'.virtualShop,#virtualSubmit',function(){
        shopDataType="VIRTUAL_GOODS";
        queryAjaxVirtualList(1);
    }).on("click","#openview",function(){

        getRedirectMenu(baseloaction+'/admin/IntegralCardVoucher/virtual/goodList',true);


    });





    /**
     * 获取优惠券列表页面
     * @param pageNo 当前页码
     */
    function queryAjaxVirtualList(pageNo) {
        var params = $("#virtual-form").serializeJSON();
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction + '/admin/IntegralCardVoucher/virtual/selectGoodAjaxList',//请求URL
            contextId: 'virtual-list',//响应内容页面设置内容ID
            params: params,//请求参数
            pageNo: pageNo,//页码
            pageCall: queryAjaxCouponList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId: '', //全选框ID
            callback: function () {//数据加载完成回调

            }
        };
        ajaxGetListData(obj)
    }



    /**
     * 获取优惠券列表页面
     * @param pageNo 当前页码
     */
    function queryAjaxCouponList(pageNo) {
        var params = $("#coupon-search-form").serializeJSON();
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction + '/admin/op/coupon/querySelectCouponListPage',//请求URL
            contextId: 'coupon-list-context',//响应内容页面设置内容ID
            params: params,//请求参数
            pageNo: pageNo,//页码
            pageCall: queryAjaxCouponList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId: '', //全选框ID
            callback: function () {//数据加载完成回调

            }
        };
        ajaxGetListData(obj)
    }


    /**
     * 礼品卡列表
     * @param pageNo
     */
    function queryAjaxGiftCardList(pageNo) {
        var params = $("#giftCard-search-form").serializeJSON();
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction + '/admin/op/giftCard/queryGiftCardSelectListPage',//请求URL
            contextId: 'giftCard-list-context',//响应内容页面设置内容ID
            params: params,//请求参数
            pageNo: pageNo,//页码
            pageCall: queryAjaxGiftCardList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId: '', //全选框ID
            callback: function () {//数据加载完成回调
            }
        };
        ajaxGetListData(obj)
    }

    /**
     * 获取课程列表
     * @param pageNo
     */
    function queryAjaxCourseList(pageNo) {
        var params = $("#course-search-form").serializeJSON();
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction + '/admin/course/cou/ajaxSelectIntegralCourseList',//请求URL
            contextId: 'course-list-context',//响应内容页面设置内容ID
            params: params,//请求参数
            pageNo: pageNo,//页码
            pageCall: queryAjaxCourseList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId: '', //全选框ID
            callback: function () {//数据加载完成回调
            }
        };
        ajaxGetListData(obj)
    }


});



function getShopData(){
    var obj ={};
    var selectData = getSelectData();
    if(selectData==null){
        return null;
    }
    obj.entity=selectData;
    obj.shopDataType=shopDataType;
    return  obj;
}


/**
 * 获取选中课程
 */
function getSelectData() {

    var input =null;
    switch (shopDataType) {
        case "COUPON":
            input = $('#coupon-list-context input.i-checks:checked');
            break;
        case "COURSE":
            input =     $('#course-list-context input.i-checks:checked');
            break;

        case "GIFT_CARD":
            input =     $('#giftCard-list-context input.i-checks:checked');
            break;
        case "VIRTUAL_GOODS":
            input =     $('#virtual-list input.i-checks:checked');
            break;

    }
    if (input.length == 0) {
        icModale(0, "请选择数据");
        return null;
    }

    var _m = $(input);
    var id = _m.val();
    var name = _m.data("name");
    var num = _m.data("num");
    var imageJson = _m.data("image");
    var  result = {
        'id': id,
        'name': name,
        'num': num,
        'imageJson':imageJson
    };
    return result;
}